import { _decorator, Component, Node,Sprite,Label ,Tween, tween, Vec3, v3,instantiate} from 'cc';
import {GameMain} from "./GameMain";
import {StartGameUI} from "./StartGameUI";
import LevelCfgItem from "./LevelCfgItem";
import {ColorTable} from "./GameConfig";
const { ccclass, property } = _decorator;

@ccclass('LevelSelItem')
export class LevelSelItem extends Component {

    public scaleNode:Node=null;
    public effectAttachNode:Node=null;

    public bgSprite:Sprite=null;
    public diffFrameSprite:Sprite=null;
    public textLabel:Label=null;
    public levelNumLabel:Label=null;
    public lineSprite:Sprite=null;

    public levelId:number=0;

    public inited:boolean=false;


    onLoad()
    {
        this.init();
    }

    public init()
    {
        this.scaleNode=this.node.getChildByName("scale");
        this.effectAttachNode=this.node.getChildByName("effectAttach");

        this.bgSprite=this.scaleNode.getChildByName("bgSprite").getComponent(Sprite);
        this.diffFrameSprite=this.scaleNode.getChildByName("diffFrame").getComponent(Sprite);
        this.textLabel=this.scaleNode.getChildByName("text").getComponent(Label);
        this.levelNumLabel=this.node.getChildByName("levelNum").getComponent(Label);
        this.lineSprite=this.node.getChildByName("line").getComponent(Sprite);
        this.inited=true;
    }

    start() {

    }

    public setLevel(level:number)
    {
        if(!this.inited)
            this.init();
        this.levelNumLabel.string=""+(level+1);
        this.levelId=level;
        this.updateBgSprite();
    }

    public scaleAnim()
    {
        this.scaleNode.setScale(0.1, 0.1, 1);
        let effect=instantiate(GameMain.instance.starExplodePrefab);
        this.effectAttachNode.addChild(effect);
        tween(this.scaleNode).to(0.7, {scale:Vec3.ONE },{easing:"backOut"}).call(()=>{

        }).start();
    }

    public updateBgSprite()
    {
        //处理level 超出配置数量的情况
        if(this.levelId >= GameMain.levelCfg.length)
        {
            this.bgSprite.spriteFrame=GameMain.instance.startGameUI.levelLockedSpriteFrame;
            this.lineSprite.color=StartGameUI.grayLineColor;
            return;
        }

        let curLevel:number=GameMain.instance.gameData.curLevelId;
        let cfg:LevelCfgItem=GameMain.levelCfg[this.levelId];
        if(this.levelId===curLevel)
        {
            this.bgSprite.spriteFrame=GameMain.instance.startGameUI.levelCurrentSpriteFrame;
            this.lineSprite.color=StartGameUI.grayLineColor;

            this.scaleNode.setScale(0.1, 0.1 ,1);
        }
        else if(this.levelId<curLevel)
        {
            this.bgSprite.spriteFrame=GameMain.instance.startGameUI.levelPassedSpriteFrame;
            this.lineSprite.color=StartGameUI.greenLineColor;
        }
        else if(this.levelId>curLevel)
        {
            this.bgSprite.spriteFrame=GameMain.instance.startGameUI.levelLockedSpriteFrame;
            this.lineSprite.color=StartGameUI.grayLineColor;
        }


        if(cfg && cfg.diff>0)
        {
            this.diffFrameSprite.node.active=true;
            this.textLabel.node.active=false;
            this.diffFrameSprite.spriteFrame=GameMain.instance.startGameUI.diffSpriteFrames[cfg.diff-1];
            if(cfg.diff===1)
            {
                this.textLabel.string = "挑战";
                this.textLabel.color=ColorTable.ChallengeColor;
            }
            else
            {
                this.textLabel.string="困难";
                this.textLabel.color=ColorTable.HardColor;
            }
        }
        else
        {
            this.diffFrameSprite.node.active=false;
            this.textLabel.node.active=false;
        }
    }

    update(deltaTime: number) {
        
    }
}

